#! /bin/bash
build_version=$1
source ../variable.sh 
version_function="set_version_${build_version}"
if declare -f "$version_function" > /dev/null; then
    $version_function
else
    echo "Version function $version_function not found. Exiting."
    exit 1
fi

#rm -f *.tar

# pull docker images.
for image_name in "${!calico_images[@]}"; do
    version_var="${calico_images[$image_name]}"
    version="${!version_var}"
    
    if [[ "$baseline" != "120" && "$image_name" == "pod2daemon-flexvol" ]]; then
        continue
    fi
    # 使用环境变量构建镜像地址
    full_image="${CALICO_REPO}/${image_name}:${version}"
    ctr images pull "$full_image" 
    ctr images export  ${image_name}-${KUBERNETES_VERSION}.tar "$full_image"
done

for image_name in "${!flannel_images[@]}"; do
    version_var="${flannel_images[$image_name]}"
    version="${!version_var}"
    
    # 使用环境变量构建镜像地址
    full_image="${IMAGE_REPO}/${image_name}:${version}"
    ctr images pull "$full_image" 
    ctr images export  ${image_name}-${KUBERNETES_VERSION}.tar "$full_image"
done

# 拉取K8s核心镜像
for image_name in "${!k8s_images[@]}"; do
    version_var="${k8s_images[$image_name]}"
    version="${!version_var}"
    
    # 使用环境变量构建镜像地址
    full_image="${IMAGE_REPO}/${image_name}:${version}"
    ctr images pull "$full_image" 
    ctr images export  ${image_name}-${KUBERNETES_VERSION}.tar "$full_image"
done

echo "All images have been pulled and saved."
